package com.example.lib_base.utils

import java.lang.ref.WeakReference
import kotlin.reflect.KProperty

//弱应用 by 委托方式
fun <T> weak(initializer: () -> T) = Weak(initializer.invoke())

class Weak<T>(r: T) {
    private var reference: WeakReference<T?> = WeakReference(r)

    operator fun getValue(thisRef: Any?, property: KProperty<*>): T? = reference.get()

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) {
        this.reference = WeakReference(value)
    }
}
